#include "faceanimationwidget.h"

#include <QPainter>
#include <QPainterPath>

#include <QDebug>

FaceAnimationWidget::FaceAnimationWidget(QWidget *parent)
    : QWidget{parent}
{
    margin = 3;
    radius = 10;
    radius_extern = 6;

    eye_blink_derection = false;
    eye_blink_min = 1;
    eye_blink_step = 2;
    eye_blink_max = 6;

    noselength = 16;
    noseradius = 5;

    mouthlength = 20;
    mouth_smell_step = 0;
    mouth_smell_max = 10;

    timerEyeAnimation = nullptr;
    timerMouthAnimation = nullptr;

}

void FaceAnimationWidget::startEyeBlinkAnimation()
{
    if (timerEyeAnimation == nullptr)
    {
        timerEyeAnimation = new QTimer(this);
        timerEyeAnimation->setInterval(80);
        connect(timerEyeAnimation, &QTimer::timeout, this, &FaceAnimationWidget::updateEyeAnimation);
    }

    timerEyeAnimation->start();
}

void FaceAnimationWidget::startMouthSmellAnimation()
{
    mouth_smell_step = 0;
    if (timerMouthAnimation == nullptr)
    {
        timerMouthAnimation = new QTimer(this);
        timerMouthAnimation->setInterval(80);
        connect(timerMouthAnimation, &QTimer::timeout, this, &FaceAnimationWidget::updateMouthAnimation);
    }

    timerMouthAnimation->start();
}

void FaceAnimationWidget::stopEyeBlinkAnimation()
{
    timerEyeAnimation->stop();
    delete timerEyeAnimation;
    timerEyeAnimation = nullptr;
}

void FaceAnimationWidget::stopMouthSmellAnimation()
{
    timerMouthAnimation->stop();
    delete timerMouthAnimation;
    timerMouthAnimation = nullptr;
}

void FaceAnimationWidget::updateEyeAnimation()
{
    if (eye_blink_step == eye_blink_max)
    {
        eye_blink_derection = false;
    }
    else if (eye_blink_step == eye_blink_min)
    {
        eye_blink_derection = true;
    }
    else
    {
        // keep
    }

    eye_blink_step += (eye_blink_derection ? 1 : (-1));
    update();
}

void FaceAnimationWidget::updateMouthAnimation()
{
    mouth_smell_step++;
    if (mouth_smell_step >= mouth_smell_max)
    {
//        qDebug() << mouth_smell_step << mouth_smell_max;
//        mouth_smell_step = mouth_smell_max;
        stopMouthSmellAnimation();
    }
    update();
}

void FaceAnimationWidget::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);

    QPen pen;
    pen.setColor(Qt::white);
    pen.setWidth(4);
    pen.setCapStyle(Qt::RoundCap);
    painter.setPen(pen);
    painter.setBrush(Qt::NoBrush);

    drawBorder(&painter);
    drawEye(&painter);
    drawNose(&painter);
    drawMouth(&painter);
}
void FaceAnimationWidget::drawBorder(QPainter *painter)
{

    painter->drawArc(QRect(rect().topLeft() + QPoint(margin, margin), QSize(radius*2, radius*2)),
                     90 * 16, 90 * 16);
    painter->drawArc(QRect(rect().bottomRight() - QPoint(radius*2+margin, radius*2+margin), QSize(radius*2, radius*2)),
                     270 * 16, 90 * 16);
    painter->drawArc(QRect(rect().topRight() - QPoint(radius*2+margin, -margin), QSize(radius*2, radius*2)),
                     0 * 16, 90 * 16);
    painter->drawArc(QRect(rect().bottomLeft() - QPoint(-margin, radius*2+margin), QSize(radius*2, radius*2)),
                     180 * 16, 90 * 16);

    QPainterPath path;
    path.moveTo(rect().topLeft() + QPoint(margin+radius, margin));
    path.lineTo(rect().topLeft() + QPoint(margin+radius + radius_extern, margin));

    path.moveTo(rect().topLeft() + QPoint(margin, margin+radius));
    path.lineTo(rect().topLeft() + QPoint(margin, margin+radius + radius_extern));

    path.moveTo(rect().topRight() - QPoint(margin+radius, -margin));
    path.lineTo(rect().topRight() - QPoint(margin+radius + radius_extern, -margin));

    path.moveTo(rect().topRight() + QPoint(-margin, margin+radius));
    path.lineTo(rect().topRight() + QPoint(-margin, margin+radius+radius_extern));

    path.moveTo(rect().bottomRight() - QPoint(margin+radius, margin));
    path.lineTo(rect().bottomRight() - QPoint(margin+radius + radius_extern, margin));

    path.moveTo(rect().bottomRight() - QPoint(margin, margin+radius));
    path.lineTo(rect().bottomRight() - QPoint(margin, margin+radius + radius_extern));

    path.moveTo(rect().bottomLeft() + QPoint(margin+radius, -margin));
    path.lineTo(rect().bottomLeft() + QPoint(margin+radius + radius_extern, -margin));

    path.moveTo(rect().bottomLeft() - QPoint(-margin, margin+radius));
    path.lineTo(rect().bottomLeft() - QPoint(-margin, margin+radius+radius_extern));

    painter->drawPath(path);

}

void FaceAnimationWidget::drawEye(QPainter *painter)
{
    QPainterPath path;
    QPoint lstart = 0.5*(rect().topLeft() + QPoint(margin, margin) + rect().center());
    path.moveTo(lstart);
    path.lineTo(lstart + QPoint(0, eye_blink_step));

    QPoint rstart = 0.5*(rect().topRight() - QPoint(margin, -margin) + rect().center());
    path.moveTo(rstart);
    path.lineTo(rstart + QPoint(0, eye_blink_step));

    painter->drawPath(path);
}
void FaceAnimationWidget::drawMouth(QPainter *painter)
{
    QPainterPath path;
    QPoint mstart = 0.5*(0.5*(rect().bottomLeft()+rect().bottomRight()) + rect().center());
    path.moveTo(mstart - QPoint(0.5*mouthlength, 0));
    path.quadTo(mstart + QPoint(0, mouth_smell_step), mstart + QPoint(0.5*mouthlength, 0));
    painter->drawPath(path);
}
void FaceAnimationWidget::drawNose(QPainter *painter)
{
    QPainterPath path;

    QPoint nend = rect().center();
    QPoint nstart = nend - QPoint(0, 0.25* (height() - 2*margin));
    path.moveTo(nstart);
    path.lineTo(nend);

    painter->drawPath(path);
    painter->drawArc(QRect(QPoint(rect().center() - QPoint(2*noseradius, noseradius)), QSize(2*noseradius, 2*noseradius)),
                     270*16, 90*16);
}
